<?php

namespace App\Http\Controllers\Api\MiniprogramToGame;

use App\Http\Controllers\Controller;
use App\Models\Log\LogPokemonVote;
use App\Services\MiniprogramToGame\PokemonVoteService;
use Illuminate\Http\Request;
use YQ\YqExtend;

class PokemonVoteController extends Controller
{
    public function index(Request $request)
    {
        $appid = $request->input('appid');
        $openid = $request->input('openid');

        $data = PokemonVoteService::getInstance()->getInfo($appid, $openid);
        return yqok($data);
    }

    public function vote(Request $request)
    {
        $appid = $request->input('appid');
        $openid = $request->input('openid');
        $item_id = $request->input('vote_item_id');

        $log = LogPokemonVote::where('openid', $openid)->where('appid', $appid)->first();
        if (!$log) {
            $log = new LogPokemonVote;
            $log->create_time = time();
            $log->appid = $appid;
            $log->openid = $openid;
            $log->item_id = $item_id;
            $log->ip = YqExtend::getIP();
            $log->save();

            PokemonVoteService::getInstance()->userVote($appid, $item_id);
        }

        $data = PokemonVoteService::getInstance()->getInfo($appid, $openid);
        return yqok($data);
    }
}
